<template>
  <div class="app-container">
    <el-card>
      <el-row type="flex" justify="end">
        <el-button size="small" type="primary" @click="addPerm(1, '0')">添加权限</el-button>
      </el-row>

      <el-table :data="permList" row-key="id" default-expand-all>
        <el-table-column label="权限名称" prop="name" />
        <el-table-column label="权限标记" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column label="操作按钮">
          <template v-slot="scoped">
            <el-button v-if="scoped.row.type === 1" type="text" @click="addPerm(2, scoped.row.id)">添加权限</el-button>
            <el-button type="text" @click="updatePerm(scoped.row.id)">编辑权限</el-button>
            <el-button type="text" @click="delPerm(scoped.row.id)">删除权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="新增权限" :visible="isShowDialog">
      <el-form ref="addPerm" label-width="80px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="激活状态">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermList, getPermDetail, updatePerm, addPerm, delPerm } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      permList: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      }
    }
  },
  created() {
    this.getPermList()
  },
  methods: {
    async getPermList() {
      // 因为是树形数据, 所以跟部门一样需要处理转换
      // 对于权限来说, 顶级 pid 是字符串 0
      this.permList = listToTreeData(await getPermList(), '0')
    },
    async updatePerm(id) {
      this.formData = await getPermDetail(id)
      this.isShowDialog = true
    },
    addPerm(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    async btnOK() {
      // 校验
      await this.$refs.addPerm.validate()
      // 发请求
      // await updatePerm(this.formData)
      if (this.formData.id) {
        // 编辑
        await updatePerm(this.formData)
      } else {
        // 新增
        await addPerm(this.formData)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 更新页面
      this.getPermList()
    },
    btnCancel() {
      // 清理数据
      this.formData = {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      }
      // 清理报错
      this.$refs.addPerm.resetFields()
      // 关闭弹窗
      this.isShowDialog = false
    },
    async delPerm(id) {
      // 二次询问
      await this.$confirm('是否确认删除')
      // 发请求
      await delPerm(id)
      // 提示用户
      this.$message.success('操作成功')
      // 更新页面
      this.getPermList()
    }
  }
}
</script>

<style>

</style>
